//
//  MessagePopView.m
//  GC_OC
//
//  Created by Macbook Pro on 2023/4/23.
//

#import "MessagePopView.h"

@interface MessagePopView ()

@property (nonatomic, weak) UIView *container;
@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIImageView *arrowView;
@property (nonatomic, weak) UILabel *contentLabel;
@property (nonatomic, weak) UIView *cover;
@property (nonatomic, assign) CGFloat containerH;

@end

@implementation MessagePopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIView *cover = [[UIView alloc] init];
    [self addSubview:cover];
    self.cover = cover;
    cover.backgroundColor = [UIColor blackColor];
    cover.alpha = 0.7;
    cover.hidden = true;
    [cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)]];
    
    UIView *container = [[UIView alloc] init];
    [self addSubview:container];
    self.container = container;
    container.backgroundColor = [UIColor whiteColor];
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"meiti"]];
    [container addSubview:iconView];
    self.iconView = iconView;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(17) textColor:fkColor333333];
    [container addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
//    UIImageView *arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"meiti"]];
//    [container addSubview:arrowView];
//    self.arrowView = arrowView;
    
    UILabel *contentLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [container addSubview:contentLabel];
    self.contentLabel = contentLabel;
    contentLabel.numberOfLines = 0;
    
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(150);
    }];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(15));
        make.left.offset(15);
        make.top.offset(12);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(6);
        make.right.offset(-15);
        make.centerY.equalTo(iconView);
    }];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.top.equalTo(iconView.mas_bottom).offset(6);
    }];
//    [arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//    }];
    [cover mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    self.hidden = true;
    self.clipsToBounds = true;
}

- (void)openWith:(CGFloat)height
{
    self.hidden = false;
    self.cover.hidden = false;
    self.fk_height = height;
    [UIView animateWithDuration:0.25 animations:^{
        [self.container mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.offset(0);
            make.height.offset(self.containerH);
        }];
    } completion:^(BOOL finished) {
    }];
}

- (void)close
{
    self.cover.hidden = true;
    [UIView animateWithDuration:0.25 animations:^{
        [self.container mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.offset(0);
            make.height.offset(0);
        }];
    } completion:^(BOOL finished) {
        self.hidden = true;
        self.fk_height = 0.01;
    }];
}

- (void)setData:(id)data
{
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        self.titleLabel.text = data[@"title"];
        self.contentLabel.text = data[@"content"];
        
        CGFloat containerH = 0;
        CGFloat iconH = fk_adjustW(15);
        CGFloat contentH = [self.contentLabel.text boundingRectWithSize:CGSizeMake(fkScreenW - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.contentLabel.font} context:nil].size.height;
        containerH = 12 + iconH + 6 + contentH + 12;
        self.containerH = containerH;
//        [self.container mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.top.left.right.offset(0);
//            make.height.offset(containerH);
//        }];
    }
}

@end
